// by Xeno
//Edited by Jon & Tankbuster
//#define __DEBUG__
#define THIS_FILE "x_m102.sqf"
#include "x_setup.sqf"
private ["_newgroup", "_civ", "_civgrp125", "_observationtime", "_waittime", "_neartanks", "_alivetanks","_nobjs", "_contact", "_timewarned", "_leader", "_poss", "_pos_other", "_pos_other2"];
_timewarned = false;
_contact = false;

_mpos = markerPos QGVAR(sm_602);// Meeting point with Resistance Contact.
_mpos set [2,0];
_mpos2 = markerPos QGVAR(sm_602_1);// Viewpoint Location to be revealed When your shown how to get there... is that possible?
_mpos2 set [2,0];
_mpos3 = markerPos QGVAR(sm_602_2);// Actual Location Of Enemy Armoured Reinforcements. Will use existing Create groups as per normal SMs
_mpos3 set [2,0];
GVAR(x_sm_pos) = [_mpos, _mpos2, _mpos3]; // index: 102,   Observe Enemy Camp then, When orders are received, Destroy Armoured Reinforcements.
GVAR(x_sm_type) = "normal"; // "convoy"
	
#ifdef __SMMISSIONS_MARKER__
if (true) exitWith {};
#endif
_waittime = (time + (25 * 60));// Setting Wait Time for initial Meet up with Resistance Contact (15minutes)

if (X_Client && {!GVAR(IS_HC_CLIENT)}) then {
	GVAR(cur_sm_txt) = (localize "STR_DOM_MISSIONSTRING_5071");// "Get to the Map Location to meet the Resistance Contact, he has information for your Team"
	GVAR(current_mission_resolved_text) = (localize "STR_DOM_MISSIONSTRING_735");//"Good Drills you have stopped the enemy Armoured Reinforcements"
};
// Create Resistance Contact for the 'Meeting' at the First marker SM_102
if (call FUNC(checkSHC)) then {
    __PossAndOther
	
	_pos_other = GVAR(x_sm_pos) select 1;
    _pos_other2 = GVAR(x_sm_pos) select 2;

	_civgrp125 = createGroup WEST;
	_civ = _civgrp125 createUnit ["B_G_Soldier_TL_F",_poss, [], 0, "NONE"];
	//removeHeadgear _civ;
	//removeUniform _civ;
	//_civ setFace 'WhiteHead_11';
	//_civ addHeadgear 'H_Cap_khaki_specops_UK';
    //_civ addVest 'V_TacVest_khk';
    //_civ addUniform 'U_B_CombatUniform_mcam_tshirt';
    //_civ addWeapon 'arifle_TRG21_F';
    //_civ addmagazines ["30Rnd_556x45_Stanag", 2];
	_civ allowFleeing 0;
	_civ disableAI "Move"; 
	_civ setBehaviour "Stealth";
	
	doStop _civ;
	_civ addEventHandler ["killed", {GVAR(sm_winner) = -1; GVAR(sm_resolved) = true;}];
	while {!GVAR(sm_resolved) && {count ((getpos _civ) nearEntities ["B_Soldier_base_F", 5]) < 1}} do {
		sleep 5.123;
		if (!_timewarned && {time > (_waittime - 180)}) then {
			[QGVAR(kbmsg), [48]] call FUNC(NetCallEventCTS);// message to Actual... "Hurry up, The Contact wont wait for ever"
			_timewarned = true;
		};
		if (time > _waittime) exitWith {//fail, "you didnt get there in time, he has left the area before being captured"
			GVAR(sm_winner) = -881;//change later....but think is time fail anyway :)
			GVAR(sm_resolved) = true;	
		};
	};
		//sleep 2.123;
	
	// You arrive at The Map Location and Meet the contact....
	if (time < _waittime && {!_contact}) then {
		_nobjs = _civ nearEntities ["CAManBase", 15];
		if (count _nobjs > 1) then {
			{
				if (isPlayer _x && {alive _x}) exitWith {
					_contact = true;
					_civ enableAI "MOVE";// This Allows him to move
					[QGVAR(setcapt), [_civ, true]] call FUNC(NetCallEvent);
					
					[QGVAR(kbmsg), [49]] call FUNC(NetCallEventCTS);//message to Actual from Resistance Contact... "Follow me, I will show you the location of Armoured Reinforcements"
				};
			} forEach _nobjs;
		};
	};	
	_civ enableAI "MOVE";// This Allows him to move
	_civ allowFleeing 0;
	_civ setSpeedMode "LIMITED";
	_civ setBehaviour "CARELESS";
	sleep 2;
	_civ doMove _pos_other;//and will lead all players in the group  to sm_102_1
	if (GVAR(sm_resolved)) exitWith {};	
	sleep 2.123;
	["specops", 3, "basic", 0, _pos_other2, 60,true] spawn FUNC(CreateInf);
	sleep 2.123;
	for "_i" from 0 to 4 do {
		_apc = createVehicle ["O_APC_Wheeled_02_rcws_F", _pos_other2, [],40,"NONE"];
		_apc setfuel 0;
		_apc lock 2;
	};
		
	// Your team and the Resistance contact Arrive at the Target Map Location
	
    while {alive _civ && {(_civ distance _pos_other) > 5}} do {
		sleep 5;
	};
 
   [QGVAR(kbmsg), [50]] call FUNC(NetCallEventCTS);//message to Actual from Resistance Contact... "We are here, Look There they are. Good Luck My friends, I must leave now"

	sleep 15.5;
   _civ doMove _poss;
   _nobjs = _civ nearEntities ["CAManBase", 35];
	 if (count _nobjs < 1) then {
		deleteVehicle _civ; // remove res contact
	};

   _observationtime = time + (5 * 60);// Setting the Observation Time of enemy reinforcements before allowing Attack Orders is 3 minutes
   sleep 25;
 
   [QGVAR(kbmsg), [51]] call FUNC(NetCallEventCTS);// message From Actual To Base " We have Visual on reinforcements Awaiting your go"
 
   sleep 30;

   [QGVAR(kbmsg), [52]] call FUNC(NetCallEventCTS);// message to Actual... "Awaiting the order for you to attack...over"
   
   sleep 120;
   
	if (time > _observationtime) then {
		[QGVAR(kbmsg), [53]] call FUNC(NetCallEventCTS);// message to Actual... "Your order is GREEN for attack...over"
		
		_timewarned = true;
	};	
 
	//completion lines ->
	while {!GVAR(sm_resolved)} do {
		sleep 5;
		_apc = _pos_other2 nearobjects  ["O_APC_Wheeled_02_base_F", 51];//Base class for Tank Substitute
		_alivetanks = {alive _x} count _apc;// Checks to see how many tanks are alive then add in win completion if all dead......
		if (_alivetanks == 0) exitWith {
			GVAR(sm_winner) = 2;
			GVAR(sm_resolved) = true;
		};
	};
};
if (GVAR(IS_HC_CLIENT)) then {
	[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
};